package com.dlg.user.rabbitmq.receiver;

import com.dlg.commons.constant.MqKeyConstants;
import com.dlg.user.domain.user.OjUser;
import com.dlg.user.domain.user.OjUserEnterpriseAgentAttribute;
import com.dlg.user.domain.user.OjUserEnterpriseAttribute;
import com.dlg.user.domain.user.OjUserPersonalAttribute;
import com.dlg.user.rpc.api.vo.user.UserRegisterRpcVo;
import com.dlg.user.service.IUserService;
import org.apache.log4j.Logger;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by zhangjinxia on 2017/8/10.
 */
@Component
public class UserMqReceiver {

    private static Logger logger = Logger.getLogger(UserMqReceiver.class);

    @Autowired
    private IUserService userService;

    @RabbitListener(queues = MqKeyConstants.register)
    public void register(UserRegisterRpcVo user) {
        try {
            OjUser user1 = userService.register(user);
            if (user.getParentUserId()!=null) {
                userService.addRelationShip(user1.getPhone(), user1.getId(), user1.getType(), user.getParentUserId());
            }
        }catch (Exception e){
            logger.error("receive mq register faild",e);
        }
        logger.info(String.format("receive register user: %s", user));
    }

    @RabbitListener(queues =  MqKeyConstants.updateUser)
    public void updateUser(OjUser user) {
        try {
            userService.updateUserById(user);
        }catch (Exception e){
            logger.error("mq updateUser faild",e);
        }
        logger.info(String.format("receive updateUser: %s", user));
    }

    @RabbitListener(queues = MqKeyConstants.updatePersonalAttribute)
    public void updatePersonalAttribute(OjUserPersonalAttribute personalAttribute) {
        try {
            userService.updatePersonalUserByUserId(personalAttribute);
        }catch (Exception e){
            logger.error("mq updatePersonalAttribute faild",e);
        }
        logger.info(String.format("receive updatePersonalAttribute: %s", personalAttribute));
    }

    @RabbitListener(queues = MqKeyConstants.updateEnterpriseAttribute)
    public void updateEnterpriseAttribute(OjUserEnterpriseAttribute enterpriseAttribute) {
        try {
            userService.updateEnterpriseUserByUserId(enterpriseAttribute);
        }catch (Exception e){
            logger.error("mq updateEnterpriseAttribute faild",e);
        }
        logger.info(String.format("receive updateEnterpriseAttribute: %s", enterpriseAttribute));
    }


    @RabbitListener(queues =  MqKeyConstants.updateEnterpriseAgent)
    public void updateEnterpriseAgent(OjUserEnterpriseAgentAttribute enterpriseAgentAttribute) {
        try {
            userService.updateEnterpriseAgentByUserId(enterpriseAgentAttribute);
        }catch (Exception e){
            logger.error("mq updateEnterpriseAgent faild",e);
        }
        logger.info(String.format("receive updateEnterpriseAgent: %s", enterpriseAgentAttribute));
    }

    @RabbitListener(queues =  MqKeyConstants.insertEnterpriseAgent)
    public void insertEnterpriseAgent(OjUserEnterpriseAgentAttribute enterpriseAgentAttribute) {
        try {
            userService.addEnterpriseAgent(enterpriseAgentAttribute);
        }catch (Exception e){
            logger.error("mq updateEnterpriseAgent faild",e);
        }
        logger.info(String.format("receive updateEnterpriseAgent: %s", enterpriseAgentAttribute));
    }
}
